package com.sac.approval.service.impl;

import com.sac.approval.domain.AttachmentDO;
import com.sac.approval.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;

/**
 * 邮件服务
 * @author xiaodong
 * @date 2018年9月18日 下午1:32:33
 * @desc
 */
@Service
public class EmailServiceImpl implements EmailService {
	
	@Autowired
	private JavaMailSender mailSender;
	
	@Autowired
	private TemplateEngine templateEngine;

	/*@Value("${spring.mail.from}")
	private String from;*/
	private String from = "492979325@qq.com";//ShiroUtils.getUser().getEmail();//;
	
	@Override
	public void sendSimpleMail(String to, String subject, String content) {
		SimpleMailMessage message = new SimpleMailMessage();
		message.setFrom(from);
		message.setTo(to);
		message.setSubject(subject);
		message.setText(content);

		mailSender.send(message);
	}

	@Override
	public void sendHtmlMail(String to, String subject, String content) {
		MimeMessage mimeMessage = this.mailSender.createMimeMessage();
		try {
			MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
			mimeMessageHelper.setFrom(from);
			mimeMessageHelper.setTo(to);
			mimeMessageHelper.setSubject(subject);
			mimeMessageHelper.setText(content);
			mailSender.send(mimeMessage);
		} catch (MessagingException e) {
		}
	}

	@Override
	public void sendTemplateMail(String to, String subject,Context context,String url, List<AttachmentDO> attachments) {
		MimeMessage mimeMessage = this.mailSender.createMimeMessage();
		try {
			MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
			mimeMessageHelper.setFrom(from);
			mimeMessageHelper.setTo(to);
			mimeMessageHelper.setSubject(subject);
			mimeMessageHelper.setText(templateEngine.process(url, context),true);
			if(attachments!=null) {
				attachments.forEach(file->{
					FileSystemResource fileSystemResource = null;
					try {
						fileSystemResource = new FileSystemResource(new File(ResourceUtils.getURL("classpath:upload").getPath()+ File.separator+file.getFileName()));
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					}
					try {
						mimeMessageHelper.addAttachment(file.getFileOriginName(), fileSystemResource);
					} catch (MessagingException e) {
						e.printStackTrace();
					}
				});
			}

			mailSender.send(mimeMessage);
			} catch (MessagingException e) {

		}
	}
	
	@Override
	public void sendAttachments(String to, String subject, String content, List<String> filePaths) {
		MimeMessage mimeMessage = this.mailSender.createMimeMessage();
		try {
			MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
			mimeMessageHelper.setFrom(from);
			mimeMessageHelper.setTo(to);
			mimeMessageHelper.setSubject(subject);
			mimeMessageHelper.setText(content, true);
			if(filePaths!=null) {
				filePaths.forEach(filePath->{
					FileSystemResource fileSystemResource = new FileSystemResource(new File(filePath));
					String fileName = filePath.substring(filePath.lastIndexOf(File.separator) + 1);
					try {
						mimeMessageHelper.addAttachment(fileName, fileSystemResource);
					} catch (MessagingException e) {
						e.printStackTrace();
					}
				});
			}
			
			mailSender.send(mimeMessage);
		} catch (MessagingException e) {
		}

	}

	@Override
	public void sendInLineResourceMail(String to, String subject, String content, String rscPath, String rscId) {
		MimeMessage mimeMessage = this.mailSender.createMimeMessage();
		try {
			MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
			mimeMessageHelper.setFrom(from);
			mimeMessageHelper.setTo(to);
			mimeMessageHelper.setSubject(subject);
			mimeMessageHelper.setText(content, true);//先添加文本。如 helper.setText("<html><body><img src='cid:identifier1234'></body></html>", true);
			FileSystemResource fileSystemResource = new FileSystemResource(new File(rscPath));
			mimeMessageHelper.addInline(rscId, fileSystemResource);//再添加内嵌资源。如 helper.addInline("identifier1234", res);
			mailSender.send(mimeMessage);
		} catch (MessagingException e) {
		}
	}

}
